package com.mars.base.common.exception;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DataBaseException extends AppException {

	private static final long serialVersionUID = 1L;
	
	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * 插入记录失败
	 */
	public static final DataBaseException INSERT_ERROR = new DataBaseException("插入记录失败") ;
	/**
	 * 批量插入记录失败
	 */
	public static final DataBaseException INSERT_BATCH_ERROR = new DataBaseException("批量插入记录失败") ;
	/**
	 * 插入或修改记录失败
	 */
	public static final DataBaseException INSERT_UPDATE_ERROR = new DataBaseException("插入或修改记录失败") ;
	/**
	 * 修改记录失败
	 */
	public static final DataBaseException UPDATE_ERROR = new DataBaseException("修改记录失败") ;
	/**
	 * 批量修改记录失败
	 */
	public static final DataBaseException UPDATE_BATCH_ERROR = new DataBaseException("批量修改记录失败") ;
	/**
	 * 删除记录失败
	 */
	public static final DataBaseException DELETE_ERROR = new DataBaseException("删除记录失败") ;
	/**
	 * 查询记录失败
	 */
	public static final DataBaseException SELECT_ERROR = new DataBaseException("查询记录失败") ;
	/**
	 * 查询记录失败
	 */
	public static final DataBaseException NOT_FOUND_STATEMENT = new DataBaseException("找不到对应的Statement") ;
	
	/**
	 * 异常描述信息
	 */
	private String msg ;
	
	public DataBaseException() {}

	public DataBaseException(String msgFormat, Object...args) {
		super(String.format(msgFormat, args));
		this.msg = String.format(msgFormat, args);
	}
	
	public DataBaseException logError() {
		this.logger.error(this.msg);
		return new DataBaseException(this.msg) ;
	}
	
	public DataBaseException logError(Exception e) {
		return errorMsg(e) ;
	}
	
	public DataBaseException errorMsg(Exception e) {
		this.logger.error(this.msg + "\r\n" + ExceptionUtils.getMessage(e));
		return new DataBaseException(this.msg + " ### " + ExceptionUtils.getRootCauseMessage(e)) ;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
}
